﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;

namespace Backend
{
    public static class Utility
    {
        public static bool IsValidEmail(string email)
        {
            return System.Text.RegularExpressions.Regex.IsMatch(email,
                @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
        }

        public static string Reverse(string text)
        {
            if (text == null) return null;

            char[] array = text.ToCharArray();
            Array.Reverse(array);
            return new string(array);
        }

        public static void SendEmailMessage(string to, string subject, string body)
        {
            try
            {
                MailMessage message = new MailMessage();
                message.To.Add(new MailAddress(to));
                message.Subject = subject;
                message.Body = body;
                message.IsBodyHtml = true;

                SmtpClient client = new SmtpClient();
                client.Send(message);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static string GetUriDirectory(string uri)
        {
            string reversedUri = Reverse(uri);
            return Reverse(reversedUri.Substring(reversedUri.IndexOf('/'), reversedUri.Length - reversedUri.IndexOf('/')));
        }

    }
}
